package com.hundredapp.oa.web.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaSign;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.repository.ClassmngManager;
import com.hundredapp.oa.repository.SignManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.repository.WorktimeManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.ClassVO;
import com.hundredapp.oa.vo.SignclassVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Signin extends ActionSupport {

	private List<OaSign> root;

	private Object success;
	private String msg;
	private int id;
	private int start;
	private int limit;
	private int total;
	private int week;
	private int classid;
	private int searchclass;
	private int searchweek;
	private int worktime;
	private int issign;
	private OaSign sign;
	private String studentlist;
	private List<SignclassVO> signclassvos;
	private List<ClassVO> classvos;
	private Timestamp signtime;

	@Autowired
	private SignManager signmanager;
	@Autowired
	private StudentManager studentmanager;
	@Autowired
	private ClassmngManager classmanager;
	@Autowired
	private WorktimeManager worktimemanager;

	public String add() {
		try {
			for (OaStudent os : classmanager.findStudent(classid)) {
				sign = new OaSign();
				sign.setCreateUserId(WebUtil.GetLoginUserId());
				sign.setWeek(week);
				sign.setClassId(classid);
				sign.setStudentId(os.getId());
				sign.setWorktime(worktime);
				boolean flag = false;
				if(studentlist.length()>0) {
					for (String sstudentid : studentlist.split(",")) {
						int studentid = Integer.parseInt(sstudentid);
						if (studentid == os.getId())
							flag = true;
					}
				}
				if (flag) {
					sign.setState(1);
				} else {
					sign.setState(0);
				}
				signmanager.add(sign);
			}
			success = true;
			msg = "考勤成功";
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
			msg = "考勤失败";
		}
		return SUCCESS;
	}

	public String all() {
		String condition = SelectType.SELECT_ALL;
		List<OaStudent> ostus = new ArrayList<OaStudent>();
		if(searchclass > 0) {
			condition += " and classId = "+searchclass;
			ostus = classmanager.findStudent(searchclass);
		}else {
			ostus = studentmanager.findAll("");
		}
		signclassvos = new ArrayList<SignclassVO>();
		for(OaStudent ost : ostus) {
			SignclassVO scvo = new SignclassVO();
			String studentName = ost.getDisplayName();
			if(studentmanager.findlesson(searchclass, ost.getId())<20){
				studentName = "<span title=\"还剩"+studentmanager.findlesson(searchclass, ost.getId())+"课时\" style=\"color:red\">"+studentName+"</span>";
			}
			scvo.setStudentName(studentName);
			scvo.setPhone(ost.getPhone());
			condition += " and studentId="+ost.getId();
			if(worktime > 0)
				condition += " and worktime="+worktime;
			for (OaSign os : signmanager.findAllSign(condition, start,
					limit)) {
				switch (os.getWeek()) {
				case 1:
					if(os.getState()==1)
						scvo.setW1("√");
					else
						scvo.setW1("×");
					break;
				case 2:
					if(os.getState()==1)
						scvo.setW2("√");
					else
						scvo.setW2("×");
					break;
				case 3:
					if(os.getState()==1)
						scvo.setW3("√");
					else
						scvo.setW3("×");
					break;
				case 4:
					if(os.getState()==1)
						scvo.setW4("√");
					else
						scvo.setW4("×");
					break;
				case 5:
					if(os.getState()==1)
						scvo.setW5("√");
					else
						scvo.setW5("×");
					break;
				case 6:
					if(os.getState()==1)
						scvo.setW6("√");
					else
						scvo.setW6("×");
					break;
				case 7:
					if(os.getState()==1)
						scvo.setW7("√");
					else
						scvo.setW7("×");
					break;
				case 8:
					if(os.getState()==1)
						scvo.setW8("√");
					else
						scvo.setW8("×");
					break;
				case 9:
					if(os.getState()==1)
						scvo.setW9("√");
					else
						scvo.setW9("×");
					break;
				case 10:
					if(os.getState()==1)
						scvo.setW10("√");
					else
						scvo.setW10("×");
					break;
				case 11:
					if(os.getState()==1)
						scvo.setW11("√");
					else
						scvo.setW11("×");
					break;
				case 12:
					if(os.getState()==1)
						scvo.setW12("√");
					else
						scvo.setW12("×");
					break;
				}
			}
			signclassvos.add(scvo);
		}
		return SUCCESS;
	}

	public String enable() {
		OaSign ssign = signmanager.findSign(String.format("id=%s",id));
		ssign.setState(1);
		signmanager.update(ssign);
		success = true;
		msg = "考勤成功";
		return SUCCESS;
	}
	
	public String disable(){
		OaSign ssign = signmanager.findSign(String.format("id=%s",id));
		ssign.setState(0);
		signmanager.update(ssign);
		success = true;
		msg = "考勤成功";
		return SUCCESS;
	}
	
	public String getissign() {
		issign=signmanager.count(" classId="+id+" and week="+week+" and worktime="+worktime);
		success=true;
		return SUCCESS;
	}
	
	public String findclass(){
		classvos = new ArrayList<ClassVO>();
		String condition = SelectType.SELECT_ALL + " and state = 1";
		for (OaClass oc : classmanager.findAll(condition)) {
			ClassVO temp = new ClassVO();
			temp.setId(oc.getId());
			temp.setAge(oc.getAge());
			temp.setAssistantName(oc.getOaUserByAssistant().getDisplayName());
			temp.setBishopName(oc.getOaUserByBishop().getDisplayName());
			temp.setClassName(oc.getClassName());
			temp.setCount(oc.getCount());
			temp.setSubject(oc.getOaSubject().getDisplayName());
			temp.setClassroom(oc.getClassroom());
			classvos.add(temp);
		}
		return SUCCESS;
	}

	public Timestamp getSigntime() {
		return signtime;
	}

	public void setSigntime(Timestamp signtime) {
		this.signtime = signtime;
	}

	public String getStudentlist() {
		return studentlist;
	}

	public void setStudentlist(String studentlist) {
		this.studentlist = studentlist;
	}

	public List<OaSign> getRoot() {
		return root;
	}

	public void setRoot(List<OaSign> root) {
		this.root = root;
	}

	public Object getSuccess() {
		return success;
	}

	public void setSuccess(Object success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public OaSign getSign() {
		return sign;
	}

	public void setSign(OaSign sign) {
		this.sign = sign;
	}

	public int getWeek() {
		return week;
	}

	public void setWeek(int week) {
		this.week = week;
	}

	public int getClassid() {
		return classid;
	}

	public void setClassid(int classid) {
		this.classid = classid;
	}

	public List<SignclassVO> getSignclassvos() {
		return signclassvos;
	}

	public void setSignclassvos(List<SignclassVO> signclassvos) {
		this.signclassvos = signclassvos;
	}

	public int getSearchclass() {
		return searchclass;
	}

	public void setSearchclass(int searchclass) {
		this.searchclass = searchclass;
	}

	public int getSearchweek() {
		return searchweek;
	}

	public void setSearchweek(int searchweek) {
		this.searchweek = searchweek;
	}

	public int getWorktime() {
		return worktime;
	}

	public void setWorktime(int worktime) {
		this.worktime = worktime;
	}

	public int getIssign() {
		return issign;
	}

	public void setIssign(int issign) {
		this.issign = issign;
	}

	public List<ClassVO> getClassvos() {
		return classvos;
	}

	public void setClassvos(List<ClassVO> classvos) {
		this.classvos = classvos;
	}
}
